package org.maciej.hib;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.maciej.dto.Address;
import org.maciej.dto.FourWheeler;
import org.maciej.dto.TwoWheeler;
import org.maciej.dto.UserDetails;
import org.maciej.dto.Vehicle;

public class HibernateTestApp {

	/**
	 * @param args
	 */
	
	/**
	 * Adnotacje
	 *  -  @Entity 	- nazwa calej klasy w obrebie jezyka HQL i nazwa tabeli
	 *  -  @Table  	- tylko nazwa tabeli w bd
	 *  -  @Column 	- dane o kolumnie
	 *  -  @Id	   	- wybranie pola jako id tabeli
	 *  -  @Embeddable	- klasa jest dodawana
	 *  -  @Embedded	- dodanie elementow klasy do biezacej klasy
	 *  -  @EmbeddedId	- dodanie elementow klasy do biezacej klasy jako id
	 *  -  @GeneratedValue 		- autogenerowanie wartosci np id
	 *  -  @AttributeOverrides 	- nadpisanie atrybutow wielu
	 *  -  @AttributeOverride 	- nadpisanie nazwy jednego atrybutu
	 *  -  @ElementCollection	- dla typow zbiorowych np set
	 *  -  @JoinTable	- nazwa tabeli tworzonej z kolekcji, nazwa kolumny z klasy
	 *  -  @GenericGenerator 	- generator
	 *  -  @CollectionId 	- ustawienie osobnego id dla nowej tabeli tworzonej z kolekcji
	 *  -  @OneToOne / @OneToMany / @ManyToOne
	 *  
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Configuration cfg = new Configuration().configure();//"hib2.cfg.xml");
		ServiceRegistry svRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		SessionFactory sessionFactory = cfg.buildSessionFactory(svRegistry);
		Session session = sessionFactory.openSession();	
		session.beginTransaction();
		
		UserDetails user = (UserDetails) session.get(UserDetails.class, 3);
		user.setUserName("Updated");
		
		/*
		 * EXAMPLE
		 * 
		UserDetails example = new UserDetails();
		example.setUserName("User 1%");
		Example ex = Example.create(example).enableLike();
		*/
		/*
		 * CRITERIA
		 * 
		Criteria criteria = session.createCriteria(UserDetails.class)
				.add(ex);
				//.addOrder(Order.desc("userId"));
				//.setProjection(Projections.property("userId"));
		List<UserDetails> users = (List<UserDetails>)criteria.list();
		 */
		/*
		 * QUERY
		 * 
		Query query = session.getNamedQuery("UserDetails.byName");// createQuery("from UserDetails where userId > 2");
		query.setString("userName", "User 9");
		List<UserDetails> users = (List<UserDetails>) query.list();*/
		
		
		session.getTransaction().commit();
		session.close();
	
		Session session2 = sessionFactory.openSession();	
		session2.beginTransaction();
		
		UserDetails user2 = (UserDetails) session2.get(UserDetails.class, 3);
		
		session2.getTransaction().commit();
		session2.close();
		/*
		 * do 20
		 * 
		Vehicle vehicle = new Vehicle();
		vehicle.setVehicleName("CAR");
		
		TwoWheeler bike = new TwoWheeler();
		bike.setVehicleName("Bike");
		bike.setSteeringHandle("Bike Steering Handle");
		
		FourWheeler car = new FourWheeler();
		car.setVehicleName("Porsche");
		car.setSteeringWheel("Car Steering Wheel");
		
		Configuration cfg = new Configuration().configure("hib2.cfg.xml");
		ServiceRegistry svRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
		SessionFactory sessionFactory = cfg.buildSessionFactory(svRegistry);
		Session session = sessionFactory.openSession();
		
		session.beginTransaction();
		session.save(vehicle);
		session.save(bike);
		session.save(car);
		session.getTransaction().commit();
		session.close();
		 */
		
		
		/*
		 * User z jednym adresem i wieloma vehicle
		 * 
		 * UserDetails userA = new UserDetails();
		userA.setUserName("User AAA");

		UserDetails userB = new UserDetails();
		userB.setUserName("User BBB");
		
		Vehicle vehicle1_AB = new Vehicle();
		vehicle1_AB.setVehicleName("CAR1 AAA BBB");
		
		Vehicle vehicle2_AB = new Vehicle();
		vehicle2_AB.setVehicleName("CAR2 AAA BBB");
		
		Vehicle vehicle3_B = new Vehicle();
		vehicle3_B.setVehicleName("CAR3 BBB");
		
		Vehicle vehicle4_A = new Vehicle();
		vehicle4_A.setVehicleName("CAR4 AAA");
		
		Address address = new Address();
		address.setCity("Poznan");
		address.setPincode("61003");
		
		userA.addVehicle(vehicle1_AB);
		userA.addVehicle(vehicle2_AB);
		userA.addVehicle(vehicle4_A);
		userA.setAddress(address);
		
		userB.addVehicle(vehicle1_AB);
		userB.addVehicle(vehicle2_AB);
		userB.addVehicle(vehicle3_B);
		userB.setAddress(address);*/
		
		
		
		/* Odczytywnie z bazy danych
		 * 
		 * user = null;
		
		session = sessionFactory.openSession();
		session.beginTransaction();
		user = (UserDetails)session.get(UserDetails.class, 2);
		System.out.println(user.getUserId() + " " + user.getUserName());*/
	}

}
